package com.ftf.tp.message.application.service.impl;

import com.ftf.tp.blog.client.dto.ArticleDTO;
import com.ftf.tp.common.constants.NoticeTargetTypeEnum;
import com.ftf.tp.message.application.adapter.rpc.ArticleRpcAdapter;
import com.ftf.tp.message.application.service.NoticeGenerateService;
import com.ftf.tp.message.domain.directMessage.dos.NoticeDO;
import com.ftf.tp.message.domain.directMessage.dos.NoticeEventDO;
import com.ftf.tp.message.domain.directMessage.dos.NoticeSettingConfigDO;
import com.ftf.tp.message.domain.directMessage.service.impl.NoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author tanghao
 * @date 2022/9/16
 */
@Service
public class SupportNoticeImpl implements NoticeGenerateService {


    @Resource
    private ArticleRpcAdapter articleRpcAdapter;

    @Resource
    private NoticeService noticeService;

    @Override
    public void generate(NoticeEventDO noticeEventDO) {
        if (noticeEventDO.getTargetType().equals(NoticeTargetTypeEnum.ARTICLE.getCode())) {

            ArticleDTO articleDTO = articleRpcAdapter.getArticleById(noticeEventDO.getTargetId());

            if (noticeService.isReceive(articleDTO.getArticleAuthorId(), 1, NoticeTargetTypeEnum.ARTICLE.getCode())) {
                return;
            }

            NoticeSettingConfigDO noticeSettingConfig = noticeService.getNoticeSettingConfig(1, NoticeTargetTypeEnum.ARTICLE.getCode());

            String content = noticeSettingConfig.getNoticeTemplate();
            content.replace("{sender_nickname}", noticeEventDO.getNickname());
            content.replace("{notice_object_about}", articleDTO.getArticleTitle());

            NoticeDO noticeDO = NoticeDO.builder()
                    .senderId(noticeEventDO.getUserId())
                    .senderNickname(noticeEventDO.getNickname())
                    .receiverId(articleDTO.getArticleAuthorId())
                    .noticeObjectId(articleDTO.getId())
                    .noticeObjectType(noticeEventDO.getTargetType())
                    .noticeObjectAbout(articleDTO.getArticleTitle())
                    .status(false)
                    .noticeContent(content)
                    .build();

            noticeService.created(noticeDO);

        }
    }
}
